﻿#include "u_ip_edit.h"

IpEdit::IpEdit(IPData ip,QWidget *parent) : MyDialog(parent)
{
    this->bean = ip;

    this->InitWidget();
    this->SetupConnections();
    this->Initialize();
    this->OnBtnResetClicked();

    this->setMaxHide();
    this->setTitle("网口设置");
    this->resize(500, 350);
}

void IpEdit::InitWidget(){
    btnEdit  = new QPushButton(QChar(0xe657) + QString(" 保存"));   btnEdit->setMinimumWidth(100);
    btnReset = new QPushButton(QChar(0xe6dc) + QString(" 重置"));   btnReset->setMinimumWidth(100);

    editKw = new QLineEdit(); editKw->setEnabled(this->bean.Kw.isEmpty());
    editIP=new QLineEdit();
    editPort=new QLineEdit("0000"); editPort->setText(QString("0000"));
//    editTempThreshold = MyGUI::GetDoubleEdit(1, 100);
//    editHumThreshold = MyGUI::GetDoubleEdit(1, 100);
//    editTempTime = MyGUI::GetSpinBox(1, 600, "秒");
//    editHumTime = MyGUI::GetSpinBox(1, 600, "秒");
    //editEnable = new QCheckBox("启用");

    QHBoxLayout *btnLayout = new QHBoxLayout();
    btnLayout->addStretch();
    btnLayout->addWidget(btnEdit);
    btnLayout->addWidget(btnReset);

    QGridLayout *layout1 = new QGridLayout();
    layout1->addWidget(new QLabel("功率:"),    1,1,1,1, Qt::AlignRight);
    layout1->addWidget(editKw,               1,2,1,1);
    layout1->addWidget(new QLabel("IP地址:"),    2,1,1,1, Qt::AlignRight);
    layout1->addWidget(editIP,               2,2,1,1);
    layout1->addWidget(new QLabel("端口号:"),   3,1,1,1, Qt::AlignRight);
    layout1->addWidget(editPort,               3,2,1,1);
    if(globalData->CheckUserRole(0)){
        layout1->addLayout(btnLayout,          5,1,1,4);
    }

    QGroupBox *groupbox1 = new QGroupBox();
    groupbox1->setStyleSheet("margin-top: 0px;");
    groupbox1->setLayout(layout1);

    //------------------------------------------------------------------

    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->addWidget(groupbox1);

    this->addLayout(mainLayout);

}

void IpEdit::SetupConnections(){
    connect(btnEdit , SIGNAL(clicked()), this, SLOT(OnBtnEditClicked()));
    connect(btnReset , SIGNAL(clicked()), this, SLOT(OnBtnResetClicked()));
}

void IpEdit::Initialize(){

}

void IpEdit::OnBtnEditClicked(){
    if(this->editKw->text().trimmed().isEmpty()){ this->editKw->setFocus(); MyGUI::ShowMessageTip("请输入功率", this); return; }
    if(this->editIP->text().trimmed().isEmpty()){ this->editIP->setFocus(); MyGUI::ShowMessageTip("请输入IP地址", this); return; }
    if(this->editPort->text().toInt()<0){this->editPort->setFocus(); MyGUI::ShowMessageTip("请输入端口号",this);return;}
    //if(this->edit->value() < 0){ this->editAddr->setFocus(); MyGUI::ShowMessageTip("请输入地址码", this); return; }

    if(!MyGUI::ShowMessageBoxQuesion("确定保存吗？", this)){
        return;
    }

    IPData info = bean;
    info.Kw=this->editKw->text().trimmed();
    info.IP=this->editIP->text().trimmed();
    info.Port=this->editPort->text().toInt();
    //info.enable = this->editEnable->isChecked();

    bool b = false;

//    if(globalData->sqlBase->GetIpDataMap().contains(info.IP)){
        b = globalData->sqlBase->UpdateIPData(info);
        globalData->SaveSysLog("修改设备", 1);
        globalData->UpdateIPData();
        emit globalData->Update();
//    }else{
//        b = globalData->sqlBase->SaveIPData(info);
//        globalData->SaveSysLog("添加设备", 1);
//    }

    if(b){
        this->close();
        MyGUI::ShowMessageBoxInfo("保存成功", this);
        globalData->InitIPData();
         //emit globalData->Update();
    }else{
        MyGUI::ShowMessageBoxError("保存失败", this);
    }
}

void IpEdit::OnBtnResetClicked(){
    IPData info = bean;

    editKw->setText(info.Kw);
    editIP->setText(info.IP);
    editPort->setText(QString::number(info.Port));
    //editEnable->setChecked(info.enable);
}
